
var app = new Vue({
    el: '#app',
    data: {
        form: {},
        dialog: {
            isShow: false,
        },
        editor: '',
    },
    mounted:function(){
        var _this = this;
        _this.editor = new wangEditor('#editor')
        _this.editor.customConfig.onchange = function(html){
            _this.form.remarks = html;
        };
        _this.editor.customConfig.menus = [
            'head',
            'bold',
            'italic',
            'underline'
        ]
        _this.editor.create();
        _this.form.id = this.getUrlKey('id');
        if (_this.form.id) {
            _this.getCommunityDetail(this.form.id)
        }
    },
    methods: {
        /**
         * 获取小区详情
         */
        getCommunityDetail: function(id) {
            var _this = this;
            $.ajax({
                url: '/community/getOne',
                type: 'get',
                data: {id: id},
                dataType: 'json',
                success: function(res) {
                    if (res.status === 0) {
                        _this.form = res.data.community;
                        _this.editor.txt.html(_this.form.remarks);
                    } else {
                        _this.$message.error(res.message)
                    }
                }
            })
        },
        /**
         * 小区保存
         */
        communitySave: function(){
            var _this = this;
            if (!_this.form.name) {
                _this.$message.error('请输入名称！')
            } else {
                $.ajax({
                    url: '/community/save',
                    type: 'post',
                    data: _this.form,
                    dataType: 'json',
                    success: function(res) {
                        if (res.status === 0) {
                            _this.$confirm('保存成功，是否返回列表页面？', '提示消息', {
                                confirmButtonText: '是',
                                cancelButtonText: '否'
                            }).then(() => {
                                window.location.href = '/'
                            }).catch(action => {
                                _this.form.id = res.data.id;
                            });
                        } else {
                            _this.$message.error(res.message)
                        }
                    }
                })
            }

        },
        /**
         * 上传成功
         */
        fileSuccess: function(response, file, fileList, field) {
            var _this = this;
            if (response.status === 0) {
                file.id = response.data.id;
                file.raw = {};
                _this.form[field] = fileList;
                _this.$message.success(response.message)
            } else {
                _this.$message.error(response.message)
            }
        },
        /**
         * 图片预览
         */
        filePreview: function(file) {
            console.log(file)
        },
        /**
         * 图片删除
         */
        fileRemove: function(file, fileList, field) {
            var _this = this;
            _this.form[field] = fileList;
            _this.$message.success('删除成功！')
        },
        fileExceed: function() {
            this.$message.warning('只允许上传一个文件！');
        }

    }
});
